#encoding=utf8
'''
Created on Nov 25, 2014

@author: jingyang <jingyang@smalltreemedia.com>
'''
import json

from django.http.response import HttpResponse, HttpResponseRedirect

from app_info.models import App, AppVersion
from sub_info.models import SubInfo, Rejection
from django.contrib.auth.decorators import login_required
import settings
from django.core.exceptions import PermissionDenied


IOS_RATING = ['4+', '9+', '12+', '17+']
GP_RATING = ['Everyone', 'Low Maturity', 'Medium Maturity', 'High Maturity']
AMZ_RATING = []


def get_rating(req):
    app_id = req.REQUEST.get('id')
    try:
        app = App.objects.get(id=app_id)
        if app.app_store == 'iTunes App Store':
            res = IOS_RATING
        elif app.app_store == 'Google Play':
            res = GP_RATING
        elif app.app_store == 'Amazon App Store':
            res = AMZ_RATING
        else:
            res = []
    except Exception, e:
        res = []
    return HttpResponse(json.dumps({'res': res}), mimetype='application/json')


def get_version_log(req):
    app_id = req.REQUEST.get('id')
    status = req.REQUEST.get('status', 'C,L')
    try:
        app = App.objects.get(id=app_id)
        a = app.appversion_set.exclude(status__in=status.split(',')).values_list('code', flat=True)
#        a = app.appversion_set.filter(status='I').values_list('code', flat=True)
#        b = SubInfo.objects.filter(app=app).values_list('version', flat=True)
#        res = list(filter(lambda x: x not in b, a))
        res = list(a)
    except Exception, e:
        res = []
    return HttpResponse(json.dumps({'res': res}), mimetype='application/json')


def get_log_type(req):
    app_id = req.REQUEST.get('id')
    version = req.REQUEST.get('version')
    try:
        app = App.objects.get(id=app_id)
        log = AppVersion.objects.get(app=app, code=version)
        res = log.type
    except Exception, e:
        res = ""
    return HttpResponse(json.dumps({'res': res}), mimetype='application/json')


def get_sub_info(req):
    sub_id = req.REQUEST.get('id')
    try:
        res = SubInfo.objects.get(id=sub_id).request_id
    except SubInfo.DoesNotExist:
        res = "???"
    return HttpResponse(json.dumps({'res': res}), mimetype='application/json')


def get_rej(req):
    rej_id = req.REQUEST.get('id')
    try:
        res = Rejection.objects.get(id=rej_id).issue_id
    except Rejection.DoesNotExist:
        res = '???'
    return HttpResponse(json.dumps({'res': res}), mimetype='application/json')


@login_required()
def go_ps(req):
    '''
    redirect to Publish Service System, no need to login
    '''
    user = req.user
    if user.is_superuser or user.groups.filter(name__in=['Designer',
                                                         'Division Head',
                                                         'Management',
                                                         'Post-production Staff',
                                                         'Project Manager']).exists():
        return HttpResponseRedirect('{ps_domain}{do_login}?email={user.email}'
                                    .format(ps_domain=settings.PS_DOMAIN,
                                            do_login=settings.PS_DO_LOGIN,
                                            user=user))
    raise PermissionDenied


#def get_request_fullfilled(req):
#    app_id = req.REQUEST.get('id')
#    res = {'status': 1}
#    try:
#        app = App.objects.get(id=app_id)
#    except App.DoesNotExist:
#        return
#    if app.iap_required == 'Y':
#        if not app.iapinfo_set.filter(status='F').exists():
#            res.update({'iap': 0, 'status': 0})
#        if not app.iapinfo_set.exists():
#            res.update({'iap': -1, 'status': 0})
#    if app.ads_required == 'Y':
#        if not app.adsinfo_set.exists():
#            res.update({'ads': -1, 'status': 0})
#    return HttpResponse(json.dumps(res), mimetype='application/json')
